<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:terms="http://purl.org/dc/terms/" xmlns:core="http://www.w3.org/2004/02/skos/core#"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:change="http://www.health.gov.au/Schema/PBS/Change#"
  xmlns:db="http://docbook.org/ns/docbook" xmlns:pbs="http://schema.pbs.gov.au"
  xmlns:rwt="http://schema.pbs.gov.au/RWT/" xmlns:elements="http://purl.org/dc/elements/1.1/">

  <db:article>
    <db:info>
      <db:title>Extract Streamlined Authority Items</db:title>

      <db:copyright>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:year>2008</db:year>
        <db:year>2007</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").
          The following terms apply to all files associated with the software unless explicitly
          disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this
          software and its documentation for any purpose, provided that existing copyright notices
          are retained in all copies and that this notice is included verbatim in any distributions.
          No written agreement, license, or royalty fee is required for any of the authorized uses.
          Modifications to this software may be copyrighted by their authors and need not follow the
          licensing terms described here, provided that the new terms are clearly indicated on the
          first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
          DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
          THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE
          BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
          TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
          NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS
          NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
          MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>1.10</db:revnumber>
          <db:date>2012-08-15</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Fixed bugs where circumstance references weren't being considered, and prevented prescribing rules in changes list from appearing.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.9.1</db:revnumber>
          <db:date>2012-07-19</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Performance optimisation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.9</db:revnumber>
          <db:date>2012-04-17</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Aligned with controlled vocabulary specification 2012-02-27.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.8</db:revnumber>
          <db:date>2011-12-06</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added program-code display</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.7</db:revnumber>
          <db:date>2011-11-24</db:date>
          <db:authorinitials>BD</db:authorinitials>
          <db:revremark>Only one row per prescribing-rule code</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.6</db:revnumber>
          <db:date>2011-10-12</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Restricted output of references to restrictions only</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.5</db:revnumber>
          <db:date>2011-08-01</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Fixed missing ')' in XPath statement</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.4</db:revnumber>
          <db:date>2011-06-24</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Upgraded for PBS XML Schema v2.5.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.3</db:revnumber>
          <db:date>2011-01-21</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Upgraded for PBS XML Schema v2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.2</db:revnumber>
          <db:date>2008-01-15</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Upgraded for PBS XML Schema v2.x.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.1</db:revnumber>
          <db:date>2007-05-03</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added inclusion of indication codes.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2007-04-16</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>
  </db:article>

  <xsl:output method="text"/>

  <xsl:param name="display">
drug-name
item-code
indication-code
</xsl:param>

  <xsl:variable name="no-contact"
    select="&quot;http://schema.pbs.gov.au/Authority/Method#streamlined&quot;"/>

  <xsl:key name="ident" match="*[not(ancestor::pbs:changes-list)]" use="@xml:id"/>

  <db:template name="root">
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML
      Schema.</db:para>
  </db:template>
  <xsl:template match="/">
    <xsl:variable name="major" select="substring-before(/*/@version, &quot;.&quot;)"/>
    <xsl:variable name="minor" select="substring-after(/*/@version, &quot;.&quot;)"/>

    <xsl:choose>
      <xsl:when test="$major >= 2 and $minor >= 4">
        <xsl:apply-templates select="*/pbs:schedule/*"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">This stylesheet is designed to operate upon a Schedule XML
          document that conforms to version 2.4 (or later) of the PBS XML Schema. See
          http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of
          this XSL stylesheet that operates on documents that conform to an earlier version of the
          schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="pbs:prescribing-rule[@type = 'streamlined' and not(ancestor::pbs:changes-list)]">

    <xsl:variable name="this" select="pbs:code"/>
    <!-- only process the first prescribing rule for the item -->
    <xsl:if test="not(preceding-sibling::pbs:prescribing-rule/pbs:code[. = $this])">

      <xsl:call-template name="display">
        <xsl:with-param name="what" select="$display"/>
      </xsl:call-template>
      <xsl:text>&#xa;</xsl:text>
    </xsl:if>
  </xsl:template>

  <db:template name="display">
    <db:refpurpose>Display values</db:refpurpose>
    <db:para>This template implements a generalised mechanism for displaying values from the data.
      The <db:literal>what</db:literal> parameter controls what values are included and in what
      order.</db:para>
  </db:template>
  <xsl:template name="display">
    <xsl:param name="what"/>
    <xsl:param name="prefix"/>

    <xsl:choose>
      <xsl:when test="normalize-space($what) = ''"/>
      <xsl:when
        test="contains($what, '&#xa;') and
                      normalize-space(substring-before($what, '&#xa;')) = ''">
        <xsl:call-template name="display">
          <xsl:with-param name="what" select="substring-after($what, '&#xa;')"/>
          <xsl:with-param name="prefix" select="$prefix"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($what, '&#xa;')">
        <xsl:call-template name="display-one">
          <xsl:with-param name="item"
            select="normalize-space(substring-before($what, '&#xa;'))"/>
          <xsl:with-param name="prefix" select="$prefix"/>
        </xsl:call-template>
        <xsl:call-template name="display">
          <xsl:with-param name="what" select="substring-after($what, '&#xa;')"/>
          <xsl:with-param name="prefix" select="'&#9;'"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="display-one">
          <xsl:with-param name="item" select="normalize-space($what)"/>
          <xsl:with-param name="prefix" select="$prefix"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <db:template name="display">
    <db:refpurpose>Display a value</db:refpurpose>
    <db:para>Display a single data value.</db:para>
  </db:template>
  <xsl:template name="display-one">
    <xsl:param name="item"/>
    <xsl:param name="prefix"/>

    <xsl:choose>
      <xsl:when test="$item = ''"/>
      <xsl:when test="$item = 'program-code'">
        <xsl:value-of select="$prefix"/>
        <xsl:apply-templates select="ancestor::pbs:listings-list/pbs:info/pbs:code" mode="textonly"
        />
      </xsl:when>
      <xsl:when test="$item = 'drug-name' and
                      pbs:ready-prepared">
        <xsl:value-of select="$prefix"/>
        <xsl:apply-templates
          select="key('ident', substring(pbs:ready-prepared/pbs:mpp-reference/@xlink:href, 2))/../db:title"
          mode="textonly"/>
      </xsl:when>
      <xsl:when test="$item = 'drug-name' and
                      pbs:infusible">
        <xsl:value-of select="$prefix"/>
        <xsl:apply-templates
          select="key('ident', substring(pbs:infusible/pbs:mp-reference/@xlink:href, 2))/db:title"
          mode="textonly"/>
      </xsl:when>
      <xsl:when
        test="$item = 'drug-name' and
                      (pbs:standard-formula-preparation |
                      pbs:drug-tariff |
                      pbs:extemporaneous-preparation)">
        <xsl:value-of select="$prefix"/>
        <xsl:apply-templates
          select="*[self::pbs:standard-formula-preparation |
                                     self::pbs:drug-tariff |
                                     self::pbs:extemporaneous-preparation]/db:title"
          mode="textonly"/>
      </xsl:when>
      <xsl:when test="$item = 'item-code'">
        <xsl:value-of select="$prefix"/>
        <xsl:apply-templates select="pbs:code" mode="textonly"/>
      </xsl:when>
      <xsl:when test="$item = 'indication-code'">
        <xsl:variable name="codes">
          <xsl:for-each select="*/rwt:restriction-references-list/rwt:restriction-reference">
            <xsl:variable name="restriction"
              select="key('ident', substring(@xlink:href, 2))[self::rwt:restriction]"/>
            <xsl:variable name="circumstances">
              <xsl:apply-templates
                select="$restriction/rwt:circumstance |
                $restriction/rwt:circumstance-reference"
                mode="circumstance"/>
            </xsl:variable>
            <xsl:if test="$restriction and normalize-space($circumstances) != ''">
              <xsl:text> </xsl:text>
              <xsl:value-of select="$restriction/pbs:code"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:variable>
        <xsl:if test="normalize-space($codes) != ''">
          <xsl:value-of select="$prefix"/>
          <xsl:value-of select="normalize-space($codes)"/>
        </xsl:if>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="rwt:circumstance" mode="circumstance">
    <xsl:if test="rwt:authority-method = 'auth:streamlined'">
      <xsl:text> streamlined</xsl:text>
    </xsl:if>
  </xsl:template>
  <xsl:template match="rwt:circumstance-reference" mode="circumstance">
    <xsl:apply-templates select="key('ident', substring(@xlink:href, 2))" mode="circumstance"/>
  </xsl:template>

  <xsl:template match="text()|@*"/>

</xsl:stylesheet>
